const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid:0,
    showQrcode:false,
    showDialog:false,
    showGroup:false,
    showApply:false,
    group:{},
    store:[],
    apply:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getParam(options,(param) =>{
      this.data.id = param.id
      this.apiGroup();
    });
  },
  //生命周期函数--监听页面显示
  onShow: function () {
    app.loginState((state)=>{
      if(state){
        this.setData({
          uid:app.globalData.loginuser.uid,
        })
      }
    },false)
  },
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    setTimeout(()=>{
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
    },1500);
    this.apiGroup();
  },
  //获取API
  apiGroup: function () {
    app.api().Get('api/v1/ais/gmgroup/read',{id: this.data.id},(result) => {
      if (result.code == 200) {
        this.setData({
          group:result.data,
        })
        this.apiGroupStore();
      }
    })
  },
  //获取API
  apiGroupStore: function () {
    app.api().Get('api/v1/ais/gmgroup/store',{id: this.data.id,type:0},(result) => {
      if (result.code == 200) {
        this.setData({
          store:result.data,
        })
      }
    })
    app.api().Get('api/v1/ais/gmgroup/store',{id: this.data.id,type:1},(result) => {
      if (result.code == 200) {
        this.setData({
          apply:result.data,
        })
      }
    })   
  },
  //商圈操作 0同意 2拒绝 3退出 4踢出
  apiGroupOperate: function (type,store_id) {
    var parms = {
      group_id:this.data.id,
      store_id:store_id,
      type:type
    }
    app.api().Get('api/v1/ais/gmgroup/operate',parms,(result) => {
      if (result.code == 200) {
        app.wxAlert(result.msg,() =>{
          if(type == 3){
            wx.navigateBack({
              delta:1
            })
          }else{
            this.apiGroup();
          }
        })
      }
    }) 
  },
  //管理操作
  showDialog: function () {
    this.setData({
      showDialog:!this.data.showDialog
    });
  },
  onDialog: function (e) {
    this.setData({showDialog:false});
    switch (e.detail.state) {
      case 0:
        wx.navigateTo({
          url: 'edit?id='+this.data.id,
        })
        break;
      case 1:
        this.apiGroupOperate(3,0);
        break;
    }
  },
  //社群操作
  showGroup: function (e) {
    this.setData({
      showGroup:!this.data.showGroup
    });
    console.log(e.currentTarget.dataset.id);
    this.data.manage_store_id = this.data.showGroup ? e.currentTarget.dataset.id : 0
  },
  onGroup: function () {
    this.setData({showGroup:false});
    this.apiGroupOperate(4,this.data.manage_store_id);
  },
  //审核操作
  showApply: function (e) {
    this.setData({
      showApply:!this.data.showApply
    });
    this.data.apply_store_id = this.data.showApply ? e.currentTarget.dataset.id : 0
  },
  onApply: function (e) {
    this.setData({showApply:false});
    switch (e.detail.state) {
      case 0:
        this.apiGroupOperate(0,this.data.apply_store_id);
        break;
      case 1:
        this.apiGroupOperate(2,this.data.apply_store_id);
        break;
    }
  },
  //一键复制事件
  copyBtn: function (e) {
    wx.setClipboardData({
      data: '输入我的商圈号【 ' + e.currentTarget.dataset.num +' 】加入和我一起生意裂变。',
      success:(res) =>{
        wx.showToast({
          title: '复制成功',
        });
      }
    });
  },
  //锁屏
  touchmove: function () {

  },
  //分享按钮
  onShareAppMessage: function () {
    console.log(this.data.id);
    return {
      title: this.data.group.group.title,
      imageUrl: this.data.group.group.face,
      desc: this.data.group.group.content,
      path: '/packageGroup/pages/store/apply?group_id='+this.data.id,
    }
  }
})